package com.dsit.hb950;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.AndroidCharacter;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MainActivity extends Activity {
    ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar)findViewById(R.id.progressBar3);
        ((TextView)findViewById(R.id.tv_android_sys_version)).setText("release:"+Build.VERSION.RELEASE);
        ((TextView)findViewById(R.id.tv_android_sys_build)).setText("api level:"+Build.VERSION.SDK_INT);
        ((TextView)findViewById(R.id.tv_revision_info)).setText(R.string.build_revision);
        Intent intent = new Intent(this,CommService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void handleOtaUpdateEvent(UpdateEvent event){
        if(event.msg.equals("loading")){
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setMax(event.total);
            progressBar.setProgress(event.current);
        }

        if(event.msg.equals("reboot")){
            progressBar.setVisibility(View.GONE);
            progressBar.setMax(0);
            progressBar.setProgress(event.current);
            Toast.makeText(this,"准备重启...",Toast.LENGTH_LONG).show();
        }

        if(event.msg.equals("error")){
            Toast.makeText(this,"升级出错...",Toast.LENGTH_LONG).show();
        }
    }
}
